package solve;

import java.util.ArrayList;
import java.util.List;

import ccore.CCore;
import ccore.model.CClass;

public class Utils {

	/* ******************************************
	 * 					CLASSES
	 * ******************************************/
	
	/**
	 * return CClass from ID
	 * @param cose id
	 */
	public static CClass getClassFromId(CCore core, int id) {
		for (CClass c: core.getCClasses()) {
			if (c.getId() == id)
				return c;
		}
		return null;
	}
	
	/**
	 * sets classes ID
	 * @param core
	 */
	public static void setClassId(CCore core) {
		List<CClass> superClasses = new ArrayList<CClass>();
		
		for (CClass c: core.getCClasses()) {
			if (c.getSuperClass() == null)
				superClasses.add(c);
		}
		
		int id = 1;
		for (CClass c: superClasses) {
			id = setClassId(c, id) + 1;
		}
	}
	
	private static int setClassId(CClass c, int id) {
		c.setId(id++);
		
		for (CClass sc: c.getSubClasses()) {
			id = setClassId(sc, id);
		}
		
		return id;
	}
	
	/**
	 * initialize classes cardinalities
	 * @param core
	 */
	public static void initClassCardinalities(CCore core) {
		for (CClass c: core.getCClasses()) {
			c.getCardinality().setLowerBound(0);
			c.getCardinality().setUpperBound(core.getInfinite());
			
			c.getSelfCardinality().setLowerBound(0);
			c.getSelfCardinality().setUpperBound(core.getInfinite());
		}
	}	
}
